//
//  WUMMyShowCaseViewController.m
//  Home
//
//  Created by 吴军虎吴军虎 on 16/5/13.
//  Copyright © 2016年 LanJue. All rights reserved.
//

#import "WUMMyShowCaseViewController.h"
#import "WUMYDetailCaseCell.h"
#import "WUEditViewController.h"
#import "YHYEditHomePageViewController.h"

@interface WUMMyShowCaseViewController ()<UITableViewDataSource,UITableViewDelegate>{
    NSIndexPath *otherIndexPath;
    
}
@property (nonatomic,retain)UITableView *myTableView;
@property (nonatomic,retain)UILabel *xiugaiL;
@property (nonatomic,retain)UILabel *removeL;
@property (nonatomic,retain) UIView *backV;
@property (nonatomic,assign)BOOL isselect;
@property (nonatomic,assign)NSInteger firsttag;
@property (nonatomic,strong)UIButton *button;

@property (nonatomic,strong)UISwipeGestureRecognizer *leftSwipeGestureRecognizer;
@property (nonatomic,strong)UISwipeGestureRecognizer *rightSwipeGestureRecognizer;


@end

@implementation WUMMyShowCaseViewController

- (void)viewWillAppear:(BOOL)animated {
    
    self.navigationController.navigationBarHidden = NO;
    self.title = @"我的案例";
    self.tabBarController.tabBar.hidden = YES;



    self.tabBarController.tabBar.hidden=YES;
    
    self.navigationController.navigationBar.translucent = YES;
    

}

-(void) viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden=NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    _isselect = YES;
    otherIndexPath = [NSIndexPath indexPathForItem:1000 inSection:0];
    
    
    //    左侧的图标
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(Back:)];
    _firsttag = 1000;
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    _xiugaiL = [UILabel new];
    _removeL = [UILabel new];
    
    [self creatView];
    // [self creatCollectionView];
    
    
    //下一步
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.backgroundColor = GH;
    [self.view addSubview:_button];
    //    self.button.layer.masksToBounds = YES;
    //    self.button.layer.cornerRadius = 5;
    [self.button setTitle:@"添加新任务" forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(goNext:) forControlEvents:UIControlEventTouchUpInside];
    [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(0);
        make.centerXWithinMargins.mas_equalTo(self.view);
        make.height.mas_equalTo(40 * AUTO_HEIGHT);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    self.button.titleLabel.font = [UIFont fontWithName:@"Arial" size:LOGINFONT * TEXTWEIGHT];
}
- (void)goNext:(UIButton *)btu{
    // NSLog(@"fwe");
    YHYEditHomePageViewController *addVC = [YHYEditHomePageViewController new];
    [self.navigationController pushViewController:addVC animated:YES];
}


- (void)rightSwipes:(UISwipeGestureRecognizer *)sender{
    CGPoint point = [sender locationInView:_myTableView];
    NSIndexPath *selectedIndex = [[NSIndexPath alloc]init];
    selectedIndex = [_myTableView indexPathForRowAtPoint:point];
    WUMYDetailCaseCell *cell = (WUMYDetailCaseCell *)[_myTableView cellForRowAtIndexPath:selectedIndex];
    NSLog(@"右边");
    //   // cell.myBackWhiteView.center=CGPointMake(SCREEN_WIDTH-cell.frame.origin.x, cell.myBackWhiteView.center.y);
    //    [UIView beginAnimations:nil context:nil];
    //    //执行动画
    //    //设置动画执行时间
    //    [UIView setAnimationDuration:0.5];
    //    //设置代理
    //    [UIView setAnimationDelegate:self];
    //    //设置动画执行完毕调用的事件
    //    //[UIView setAnimationDidStopSelector:@selector(didStopAnimation)];
    //    cell.myBackWhiteView.center=CGPointMake( (SCREEN_WIDTH- 187)* AUTO_WIDTH, cell.myBackWhiteView.center.y);
    //
    //
    //    [UIView commitAnimations];
    //     [_xiugaiL removeFromSuperview];
    //    [_removeL removeFromSuperview];
    //    [_backV removeFromSuperview];
    //
    [self remove:cell];
    
    
}


- (void)leftSwipes:(UISwipeGestureRecognizer *)sender

{
    CGPoint point = [sender locationInView:_myTableView];
    NSIndexPath *selectedIndex = [[NSIndexPath alloc]init];
    selectedIndex = [_myTableView indexPathForRowAtPoint:point];
    WUMYDetailCaseCell *cell = (WUMYDetailCaseCell *)[_myTableView cellForRowAtIndexPath:selectedIndex];
    WUMYDetailCaseCell *cell1 = (WUMYDetailCaseCell *)[_myTableView cellForRowAtIndexPath:otherIndexPath];
    
    
    
    otherIndexPath =[ NSIndexPath indexPathForItem:_firsttag inSection:0];
    WUMYDetailCaseCell *cell2 = (WUMYDetailCaseCell *)[_myTableView cellForRowAtIndexPath:otherIndexPath];
    
    if (_firsttag != 1000) {
        if (_firsttag) {
            
            if (otherIndexPath != selectedIndex) {
                [self remove:cell];
                //1
                [self remove:cell1];
                [self remove:cell2];
                
                [self creatRemoveCell:cell];
            }
            else{
                [self remove:cell];
                
                [self remove:cell1];
                [self remove:cell2];
                [self creatRemoveCell:cell];
            }
        }
        else{
            //            NSLog(@"wqd");
            //            [self remove:cell1];
            //            [self remove:cell];
            //            [self remove:cell2];
            //            [self creatRemoveCell:cell];
            if (otherIndexPath != selectedIndex) {
                [self remove:cell1];
                [self remove:cell2];
                [self remove:cell];
                [self creatRemoveCell:cell];
            }
            else{
                [self remove:cell];
                
                [self remove:cell1];
                [self remove:cell2];
                [self creatRemoveCell:cell];
            }
            
            
        }
        otherIndexPath = selectedIndex;
    }
    else{
        
        
        //
        
        // [self creatRemoveCell:cell];
        //如果向左移动，就要让其他的cell还原
        if (otherIndexPath.row != 1000) {
            if (otherIndexPath != selectedIndex) {
                [self remove:cell1];
                [self remove:cell2];
                [self creatRemoveCell:cell];
            }
            else{
                [self creatRemoveCell:cell];
            }
            
        }
        else{
            NSLog(@"asds");
            [self remove:cell1];
            //第一步
            //        [self removeRight:cell];
            //        [self removeRight:cell1];
            [self remove:cell];
            [self remove:cell1];
            [self remove:cell2];
            //[self removeRight:cell];
            [self creatRemoveCell:cell];
            
            
            
        }
        
        otherIndexPath = selectedIndex;
        // [_myTableView reloadData];
        
    }
    
}

- (void)remove:(WUMYDetailCaseCell*)cell{
    //UIButton
    //首尾式动画
    [UIView beginAnimations:nil context:nil];
    //执行动画
    //设置动画执行时间
    [UIView setAnimationDuration:0.5];
    //设置代理
    [UIView setAnimationDelegate:self];
    //设置动画执行完毕调用的事件
    // [UIView setAnimationDidStopSelector:@selector(didStopAnimation)];
    //cell.myBackWhiteView.center=CGPointMake( (SCREEN_WIDTH- 133 * AUTO_WIDTH)* AUTO_WIDTH, cell.myBackWhiteView.center.y);
    cell.myBackWhiteView.center=CGPointMake( (cell.myBackWhiteView.frame.size.width- 171 * AUTO_WIDTH), cell.myBackWhiteView.center.y);
    
    [UIView commitAnimations];
    
    //    [_xiugaiL removeFromSuperview];
    //    [_removeL removeFromSuperview];
    [_backV removeFromSuperview];
    // [_myTableView reloadData];
}
- (void)tapAction:(UITapGestureRecognizer *)tap{
    
    CGPoint point = [tap locationInView:_myTableView];
    NSIndexPath *selectedIndex = [[NSIndexPath alloc]init];
    selectedIndex = [_myTableView indexPathForRowAtPoint:point];
    WUMYDetailCaseCell *cell = (WUMYDetailCaseCell *)[_myTableView cellForRowAtIndexPath:selectedIndex];
    //首尾式动画
    [UIView beginAnimations:nil context:nil];
    //执行动画
    //设置动画执行时间
    [UIView setAnimationDuration:2.0];
    //设置代理
    [UIView setAnimationDelegate:self];
    //设置动画执行完毕调用的事件
    //  [UIView setAnimationDidStopSelector:@selector(didStopAnimation)];
    cell.myBackWhiteView.center=CGPointMake(100 * AUTO_WIDTH, cell.myBackWhiteView.center.y);
    
    [UIView commitAnimations];
    
    
    
}
#pragma mark - 编辑button
- (void)button:(UIButton *)btu{
    NSIndexPath *index = [NSIndexPath indexPathForItem:btu.tag inSection:0];
    WUMYDetailCaseCell *cell = (WUMYDetailCaseCell *)[_myTableView cellForRowAtIndexPath:index];
    NSIndexPath *index1 = [NSIndexPath indexPathForItem:_firsttag inSection:0];
    WUMYDetailCaseCell *cell1 = (WUMYDetailCaseCell *)[_myTableView cellForRowAtIndexPath:index1];
    //移动其他cell
    WUMYDetailCaseCell *cellRemove = (WUMYDetailCaseCell *)[_myTableView cellForRowAtIndexPath:otherIndexPath];
    
    
    
    
    // otherIndexPath =[ NSIndexPath indexPathForItem:_firsttag inSection:0];
    // _firsttag = otherIndexPath.row;
    WUMYDetailCaseCell *cell2 = (WUMYDetailCaseCell *)[_myTableView cellForRowAtIndexPath:otherIndexPath];
    
    
    // NSLog(@"点击%ld",btu.tag);
    
    if (!otherIndexPath) {
        NSLog(@"wq2es");
        [self removeRight:cell];
        [self remove:cell];
        [self remove:cell1];
        [self removeRight:cell1];
        
    }
    else{
        //         YHYMYDetailCaseCell *cellRemove = (YHYMYDetailCaseCell*)[_myTableView cellForRowAtIndexPath:otherIndexPath];
        //        [self remove:cellRemove];
        //        [self remove:cell1];
        //        [self remove:cell];
        //       // [self creatRemoveCell:cell];
        
        
        
        
        
        if (_firsttag != 1000) {
            
            
            if (_firsttag != btu.tag) {
                //            //其他的移动回去 第一步
                //            [self removeRight:cell1];
                //1
                [self remove:cell];
                [self remove:cell1];
                [self remove:cell2];
                [self creatRemoveCell:cell];
                //[self remove:cellRemove];
                
                
                
                
            }
            else{
                
                //再次点击统一个button
                [self remove:cell];
                [self remove:cell1];
                [self remove:cell2];
                //[self removeRight:cell1];
                [self creatRemoveCell:cell];
                // [self removeRight:cell];
            }
            
        }
        else{
            [self remove:cell1];
            //第一步
            [self removeRight:cellRemove];
            [self removeRight:cell1];
            [self remove:cell];
            [self remove:cellRemove];
            //[self removeRight:cell];
            [self creatRemoveCell:cell];
            
            
            
        }
        _firsttag = btu.tag;
        //第二步
        // [self creatRemoveCell:cell];
        
        
    }
    
}




- (void)creatRemoveCell:(WUMYDetailCaseCell *)cell{
    //首尾式动画
    [UIView beginAnimations:nil context:nil];
    //执行动画
    //设置动画执行时间
    [UIView setAnimationDuration:0.2];
    //设置代理
    [UIView setAnimationDelegate:self];
    //设置动画执行完毕调用的事件
    //  [UIView setAnimationDidStopSelector:@selector(didStopAnimation)];
    cell.myBackWhiteView.center=CGPointMake(130 * AUTO_WIDTH, cell.myBackWhiteView.center.y);
    [UIView commitAnimations];
    
    
    
    _backV = [UIView new];
    _backV.backgroundColor = GH;
    [cell addSubview:_backV];
    _backV.layer.masksToBounds = YES;
    [_backV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(cell.myBackWhiteView.mas_top);
        make.left.mas_equalTo(cell.myBackWhiteView.mas_right).offset(-50 * AUTO_WIDTH);
        make.width.mas_equalTo(50 * AUTO_WIDTH);
        make.height.mas_equalTo(cell.myBackWhiteView);
        
    }];
    
    
    [UIView beginAnimations:nil context:nil];
    //执行动画
    //设置动画执行时间
    [UIView setAnimationDuration:0.4];
    //设置代理
    [UIView setAnimationDelegate:self];
    //设置动画执行完毕调用的事件
    //  [UIView setAnimationDidStopSelector:@selector(didStopAnimation)];
    _backV.center=CGPointMake(-40, 0);
    [UIView commitAnimations];
    
    _backV.layer.cornerRadius = 5 * AUTO_WIDTH;
    
    
    
    _xiugaiL.text = @"修改";
    _xiugaiL.textColor = [UIColor whiteColor];
    _xiugaiL.font = [UIFont fontWithName:TFont size:TEXTCONTENTFONT2 * AUTO_WIDTH];
    _xiugaiL.textAlignment = NSTextAlignmentCenter;
    [_backV addSubview:_xiugaiL];
    [_xiugaiL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.mas_equalTo(_backV);
        make.height.mas_equalTo(_backV.mas_height).multipliedBy(0.5);
        make.left.mas_equalTo(_backV);
        // make.bottom.mas_equalTo(_removeL.mas_bottom);
        // make.height.mas_equalTo(10);
        
    }];
    _xiugaiL.userInteractionEnabled = YES;
    _backV.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Action:)];
    _xiugaiL.tag = 1001;
    [_xiugaiL addGestureRecognizer:tap];
    
    UIView *line = [UIView new];
    [_backV addSubview:line];
    line.backgroundColor = [UIColor whiteColor];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerXWithinMargins.mas_equalTo(_backV);
        make.centerYWithinMargins.mas_equalTo(_backV);
        make.height.mas_equalTo(1);
        make.left.mas_equalTo(_backV);
        
    }];
    
    
    
    
    
    
    
    _removeL.text = @"删除";
    _removeL.font = [UIFont fontWithName:TFont size:TEXTCONTENTFONT2 * AUTO_WIDTH];
    _removeL.backgroundColor = [UIColor redColor];
    _removeL.textColor = [UIColor whiteColor];
    _removeL.layer.masksToBounds = YES;
    _removeL.textAlignment = NSTextAlignmentCenter;
    [_backV addSubview:_removeL];
    [_removeL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_xiugaiL.mas_bottom);
        //make.height.mas_equalTo(backV.frame.size.height/2);
        make.left.mas_equalTo(_backV);
        make.width.mas_equalTo(_backV);
        make.bottom.mas_equalTo(_backV.mas_bottom);
        
        
    }];
    _removeL.userInteractionEnabled = YES;
    _removeL.tag = 1002;
    UITapGestureRecognizer *tapRemove = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(RemoveAction:)];
    [_removeL addGestureRecognizer:tapRemove];
    
}
- (void)removeRight:(WUMYDetailCaseCell *)cell{
    //其他的移动回去
    
    [UIView beginAnimations:nil context:nil];
    //执行动画
    //设置动画执行时间
    [UIView setAnimationDuration:0.5];
    //设置代理
    [UIView setAnimationDelegate:self];
    //设置动画执行完毕调用的事件
    //[UIView setAnimationDidStopSelector:@selector(didStopAnimation)];
    // cell.myBackWhiteView.center=CGPointMake( (SCREEN_WIDTH- 226)* AUTO_WIDTH, cell.myBackWhiteView.center.y);
    cell.myBackWhiteView.center=CGPointMake( (SCREEN_WIDTH- 188)* AUTO_WIDTH, cell.myBackWhiteView.center.y);
    [UIView commitAnimations];
    
    [_xiugaiL removeFromSuperview];
    [_removeL removeFromSuperview];
    [_backV removeFromSuperview];
    
    
}
- (void)RemoveAction:(UITapGestureRecognizer *)tap{
    NSLog(@"删除");
    
    
    
    CGPoint point = [tap locationInView:_removeL];
    // NSIndexPath *index = [NSIndexPath indexPathForItem:btu.tag inSection:0];
    NSIndexPath *indexPath = [[NSIndexPath alloc]init];
    indexPath = [_myTableView indexPathForRowAtPoint:point];
    // YHYMYDetailCaseCell *cell = (YHYMYDetailCaseCell*)[_myTableView cellForRowAtIndexPath:indexPath];
    
    
    
    WUMYDetailCaseCell *cell = (WUMYDetailCaseCell *)[_myTableView cellForRowAtIndexPath:indexPath];
    NSIndexPath *index1 = [NSIndexPath indexPathForItem:_firsttag inSection:0];
    WUMYDetailCaseCell *cell1 = (WUMYDetailCaseCell *)[_myTableView cellForRowAtIndexPath:index1];
    //移动其他cell
    WUMYDetailCaseCell *cellRemove = (WUMYDetailCaseCell *)[_myTableView cellForRowAtIndexPath:otherIndexPath];
    
    
    
    [self remove:cell];
    [self remove:cell1];
    [self remove:cellRemove];
    
    
    
    //UIButton
    //    //首尾式动画
    //    [UIView beginAnimations:nil context:nil];
    //    //执行动画
    //    //设置动画执行时间
    //    [UIView setAnimationDuration:0.5];
    //    //设置代理
    //    [UIView setAnimationDelegate:self];
    //    //设置动画执行完毕调用的事件
    //    [UIView setAnimationDidStopSelector:@selector(didStopAnimation)];
    //  //  cell.myBackWhiteView.center=CGPointMake( (SCREEN_WIDTH- 226)* AUTO_WIDTH, cell.myBackWhiteView.center.y);
    //    cell.myBackWhiteView.center=CGPointMake( (SCREEN_WIDTH- 187)* AUTO_WIDTH, cell.myBackWhiteView.center.y);
    //
    //    [UIView commitAnimations];
    //
    //    [_backV removeFromSuperview];
    
    [_myTableView reloadData];
    
    
}
#pragma mark - 修改
- (void)Action:(UITapGestureRecognizer *)tap{
    
    NSLog(@"修改");
    
    YHYEditHomePageViewController *addVC = [YHYEditHomePageViewController new];
    [self.navigationController pushViewController:addVC animated:YES];
    
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"cdc");
//}


- (void)creatView{
    self.view.backgroundColor = LV;
    
    _myTableView = [UITableView new];
    _myTableView.backgroundColor = LV;
    [self.view addSubview:_myTableView];
    [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view);

        make.height.mas_equalTo(self.view.frame.size.height -42 *AUTO_HEIGHT);

        
    }];
    
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    [_myTableView registerClass:[WUMYDetailCaseCell class] forCellReuseIdentifier:@"YHYMyShowCaseTableViewCell"];
    
    //去掉线
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    
    self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipes:)];
    self.rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipes:)];
    
    self.leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    // self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    
    [self.myTableView addGestureRecognizer:self.leftSwipeGestureRecognizer];
    [self.myTableView addGestureRecognizer:self.rightSwipeGestureRecognizer];
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *str = @"YHYMyShowCaseTableViewCell";
    WUMYDetailCaseCell *cell = [tableView dequeueReusableCellWithIdentifier:str forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    _bianji = [UIButton new];
    [_bianji setImage:[UIImage imageNamed:@"bibi"] forState:UIControlStateNormal];
    //[_bianji setTitle:@"编辑" forState:UIControlStateNormal];
    //[_bianji setTitleColor:ZI forState:UIControlStateNormal];
    //_bianji.titleLabel.font = [UIFont fontWithName:TFont size:TEXTALITTLEFONT * AUTO_WIDTH];
    [cell.myBackWhiteView addSubview:_bianji];
    _bianji.tag = indexPath.row;
    //_bianji.selected = YES;
    
    [_bianji addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    [_bianji mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(35/3 * AUTO_HEIGHT);
        make.right.mas_equalTo(-38/3 * AUTO_WIDTH);
        //make.height.mas_equalTo();
        make.size.mas_equalTo(CGSizeMake(15 *AUTO_WIDTH, 15 * AUTO_HEIGHT));
        
    }];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WUEditViewController *editDetail = [WUEditViewController new];
    [self.navigationController pushViewController:editDetail animated:YES];
}

#pragma mark--系统编辑按钮触发的方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    //设置tableView编辑状态，随按钮状态改变而改变
    [self.myTableView setEditing:editing animated:animated];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 6;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 300/3 * AUTO_HEIGHT;
}


-(void)Back:(UIButton *) goback
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
